/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/




#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>

#include "ps3x8.h"

#define BAUDRATE B9600


// {{{ ps3x8_connect()
int ps3x8_connect(const char *serial)
{
   int fd;
   fd_set wfds, rfds;
   struct timeval tv;
   struct termios oldtio, newtio;

   if( (fd = open(serial, O_RDWR | O_NOCTTY)) == -1)
   {
      perror("open()");
      return -1; 
   }

   if(tcgetattr(fd, &oldtio) < 0)
   {
      perror("tcgetattr()");
      return -1; 
   }

   memset(&newtio, 0, sizeof(newtio));
   newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
   newtio.c_cflag &= ~CSTOPB;
   newtio.c_cflag &= ~PARENB;
   newtio.c_cflag &= ~CRTSCTS;
   newtio.c_iflag = IGNPAR;
   newtio.c_oflag = 0;
   newtio.c_lflag = 0;
   newtio.c_cc[VTIME]    = 0;   // inter-character timer unused 
   newtio.c_cc[VMIN]     = 0;   // blocking read until X chars received 


   if(tcflush(fd, TCIFLUSH) < 0)
   {
      perror("tcflush()");
      return -1;
   }

   if(tcsetattr(fd, TCSANOW, &newtio) < 0)
   {
      perror("tcsetattr()");
      return -1;
   }
	

   // -- Escritura --
   FD_ZERO(&wfds);
   FD_SET(fd, &wfds);
   tv.tv_sec = 5;
   tv.tv_usec = 0;

   if(select(fd+1, NULL, &wfds, NULL, &tv))
   { 
      FD_CLR(fd, &wfds);
      unsigned char buf[8];
      buf[0]=0x06;
      buf[1]=0x00;
      if( write(fd, buf, 1) <=0)
      {
         perror("write()");
         return -1;
      }
   }
   else
   {
      perror("select()");
      return -1;
   }

   // -- Lectura --
   FD_ZERO(&rfds);
   FD_SET(fd, &rfds);
   tv.tv_sec = 5;
   tv.tv_usec = 0;

   if(select(fd+1, &rfds, NULL, NULL, &tv))
   { 
      FD_CLR(fd, &rfds);
      unsigned char buf[8];
      buf[0]=0;
      if(read(fd, buf, 8) < 0)
      {
         perror("read");
         return -1;
      }

      if(buf[0]!=0x06)
      {
         fprintf(stderr, "TX error, received != 0x06\n");
         return -1;
      }
   }
   else
   {
      fprintf(stderr, "TX error\n");
      return -1;
   }

   return fd;
}
// }}}

// {{{ ps3x8_disconnect()
void ps3x8_disconnect(int fd)
{
   if(close(fd)==-1)
   {
      perror("close()");
   }
}
// }}}

// {{{ ps3x8_read_digital_data()
int ps3x8_read_digital_data(int fd, unsigned int *data)
{
   fd_set wfds, rfds;
   struct timeval tv;

   if(fd==-1)
   {
      fprintf(stderr, "fd == -1 ");
      return -1;
   }

   // -- Escritura --
   FD_ZERO(&wfds);
   FD_SET(fd, &wfds);
   tv.tv_sec = 5;
   tv.tv_usec = 0;

   if (select(fd+1, NULL, &wfds, NULL, &tv))
   { 
      FD_CLR(fd, &wfds);
      unsigned char buf[8];
      buf[0]=0x01;
      buf[1]=0x00;
      if(write(fd, buf, 1) <= 0)
      {
         perror("write()");
         return -1;
      }
   }
   else
   {
      perror("select()");
      return -1;
   }

   // -- Lectura --
   FD_ZERO(&rfds);
   FD_SET(fd, &rfds);
   tv.tv_sec = 5;
   tv.tv_usec = 0;

   if (select(fd+1, &rfds, NULL, NULL, &tv))
   { 
      FD_CLR(fd, &rfds);
      unsigned char buf[8];
      buf[0]=0;
      if(read(fd, buf, 8) < 0)
      {
         perror("read()");
         return -1;
      }

      *data = buf[0];
   }
   else 
   {
      perror("select()");
   }

   return 0;
}
// }}}

// {{{ ps3x8_write_digital_data()
int ps3x8_write_digital_data(int fd, unsigned int data)
{
   fd_set wfds, rfds;
   struct timeval tv;

   if(fd==-1)
   {
      fprintf(stderr, "fd == -1");
      return -1;
   }

   // -- Escritura --
   FD_ZERO(&wfds);
   FD_SET(fd, &wfds);
   tv.tv_sec = 5;
   tv.tv_usec = 0;

   if (select(fd+1, NULL, &wfds, NULL, &tv))
   { 
      FD_CLR(fd, &wfds);
      unsigned char buf[8];
      buf[0]=0x03;
      buf[1]=data;
      buf[2]=0x00;
      if(write(fd, buf, 2) < 0)
      {
         perror("write()");
         return -1;
      }
   }
   else
   {
      perror("select()");
      return -1;
   }

   // -- Lectura --
   FD_ZERO(&rfds);
   FD_SET(fd, &rfds);
   tv.tv_sec = 5;
   tv.tv_usec = 0;

   if (select(fd+1, &rfds, NULL, NULL, &tv))
   { 
      FD_CLR(fd, &rfds);
      unsigned char buf[8];
      buf[0]=0;
      if(read(fd, buf, 8) < 0)
      {
         perror("read()");
         return -1;
      }

      if(buf[0]!=data)
      {
         fprintf(stderr, "RX error");
      }
   }
   else
   {
      perror("select()");
      return -1; 
   }

   return 0;
}
// }}}

// {{{ TEST
#ifdef TEST 
int main(int argc, char *argv[])
{
	unsigned int data=0;
	int ret;

	if(argc!=2)	
	{
		printf("Usage: %s [serial port]\n\n", argv[1]);
		return 0;
	}

   int dev = ps3x8_connect(argv[1]);
	if(dev<0)
	{
		printf("ps3x8_connect() error\n");
		return 0;
	}

	printf("STATUS: %d%d%d%d%d%d%d%d\n", 
		PS3X8_GET_IO1(data), PS3X8_GET_IO2(data), PS3X8_GET_IO3(data),
		PS3X8_GET_IO4(data), PS3X8_GET_IO5(data), PS3X8_GET_IO6(data),
		PS3X8_GET_IO7(data), PS3X8_GET_IO8(data));


	sleep(1);
   PS3X8_SET_IO1(1, data);
   PS3X8_SET_IO3(1, data);

   ret = ps3x8_write_digital_data(dev, data);
	if(ret!=0)
	{
		printf("ps3x8_write_digital_data() error\n");
		return 0;
	}

	printf("STATUS: %d%d%d%d%d%d%d%d\n", 
		PS3X8_GET_IO1(data), PS3X8_GET_IO2(data), PS3X8_GET_IO3(data),
		PS3X8_GET_IO4(data), PS3X8_GET_IO5(data), PS3X8_GET_IO6(data),
		PS3X8_GET_IO7(data), PS3X8_GET_IO8(data));




	sleep(1);
   PS3X8_SET_IO1(0, data);

   ret = ps3x8_write_digital_data(dev, data);
	if(ret!=0)
	{
		printf("ps3x8_write_digital_data() error\n");
		return 0;
	}

	printf("STATUS: %d%d%d%d%d%d%d%d\n", 
		PS3X8_GET_IO1(data), PS3X8_GET_IO2(data), PS3X8_GET_IO3(data),
		PS3X8_GET_IO4(data), PS3X8_GET_IO5(data), PS3X8_GET_IO6(data),
		PS3X8_GET_IO7(data), PS3X8_GET_IO8(data));



	sleep(1);
   PS3X8_SET_IO1(1, data);

   ret = ps3x8_write_digital_data(dev, data);
	if(ret!=0)
	{
		printf("ps3x8_write_digital_data() error\n");
		return 0;
	}

	printf("STATUS: %d%d%d%d%d%d%d%d\n", 
		PS3X8_GET_IO1(data), PS3X8_GET_IO2(data), PS3X8_GET_IO3(data),
		PS3X8_GET_IO4(data), PS3X8_GET_IO5(data), PS3X8_GET_IO6(data),
		PS3X8_GET_IO7(data), PS3X8_GET_IO8(data));


	sleep(1);
   PS3X8_SET_IO1(0, data);
   PS3X8_SET_IO3(0, data);

   ret = ps3x8_write_digital_data(dev, data);
	if(ret!=0)
	{
		printf("ps3x8_write_digital_data() error\n");
		return 0;
	}

	printf("STATUS: %d%d%d%d%d%d%d%d\n", 
		PS3X8_GET_IO1(data), PS3X8_GET_IO2(data), PS3X8_GET_IO3(data),
		PS3X8_GET_IO4(data), PS3X8_GET_IO5(data), PS3X8_GET_IO6(data),
		PS3X8_GET_IO7(data), PS3X8_GET_IO8(data));






	ps3x8_disconnect(dev);

	return 0;
}
#endif
// }}}





